home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / GDIMETA.PAK / MISC.C < prev    next >
C/C++ Source or Header  |  1997-05-06  |  3KB  |  99 lines

  1. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
  2. // ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
  3. // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  4. // PARTICULAR PURPOSE.
  5. //
  6. // Copyright (C) 1993-1995  Microsoft Corporation.  All Rights Reserved.
  7. //
  8. //  MODULE:   misc.c
  9. //
  10. //  PURPOSE:  Contains all helper functions "global" to the application.
  11. //
  12. //  FUNCTIONS:
  13. //    CenterWindow - Center one window over another.
  14. //
  15. //  COMMENTS:
  16. //
  17. //
  18.  
  19. #include <windows.h>            // required for all Windows applications
  20. #include <windowsx.h>
  21. #include <commctrl.h>
  22. #include "globals.h"            // prototypes specific to this application
  23.  
  24.  
  25.  
  26. //
  27. //  FUNCTION: CenterWindow(HWND, HWND)
  28. //
  29. //  PURPOSE:  Center one window over another.
  30. //
  31. //  PARAMETERS:
  32. //    hwndChild - The handle of the window to be centered.
  33. //    hwndParent- The handle of the window to center on.
  34. //
  35. //  RETURN VALUE:
  36. //
  37. //    TRUE  - Success
  38. //    FALSE - Failure
  39. //
  40. //  COMMENTS:
  41. //
  42. //    Dialog boxes take on the screen position that they were designed
  43. //    at, which is not always appropriate. Centering the dialog over a
  44. //    particular window usually results in a better position.
  45. //
  46.  
  47. BOOL CenterWindow(HWND hwndChild, HWND hwndParent)
  48. {
  49.     RECT    rcChild, rcParent;
  50.     int     cxChild, cyChild, cxParent, cyParent;
  51.     int     cxScreen, cyScreen, xNew, yNew;
  52.     HDC     hdc;
  53.  
  54.     // Get the Height and Width of the child window
  55.     GetWindowRect(hwndChild, &rcChild);
  56.     cxChild = rcChild.right - rcChild.left;
  57.     cyChild = rcChild.bottom - rcChild.top;
  58.  
  59.     // Get the Height and Width of the parent window
  60.     GetWindowRect(hwndParent, &rcParent);
  61.     cxParent = rcParent.right - rcParent.left;
  62.     cyParent = rcParent.bottom - rcParent.top;
  63.  
  64.     // Get the display limits
  65.     hdc = GetDC(hwndChild);
  66.     cxScreen = GetDeviceCaps(hdc, HORZRES);
  67.     cyScreen = GetDeviceCaps(hdc, VERTRES);
  68.     ReleaseDC(hwndChild, hdc);
  69.  
  70.     // Calculate new X position, then adjust for screen
  71.     xNew = rcParent.left + ((cxParent - cxChild) / 2);
  72.     if (xNew < 0)
  73.     {
  74.         xNew = 0;
  75.     }
  76.     else if ((xNew + cxChild) > cxScreen)
  77.     {
  78.         xNew = cxScreen - cxChild;
  79.     }
  80.  
  81.     // Calculate new Y position, then adjust for screen
  82.     yNew = rcParent.top  + ((cyParent - cyChild) / 2);
  83.     if (yNew < 0)
  84.     {
  85.         yNew = 0;
  86.     }
  87.     else if ((yNew + cyChild) > cyScreen)
  88.     {
  89.         yNew = cyScreen - cyChild;
  90.     }
  91.  
  92.     // Set it, and return
  93.     return SetWindowPos(hwndChild,
  94.                         NULL,
  95.                         xNew, yNew,
  96.                         0, 0,
  97.                         SWP_NOSIZE | SWP_NOZORDER);
  98. }
  99.